package com.ruoyi.contract.utlis;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.ruoyi.contract.pojo.Contract;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PDFUtli {
	//pdf文件模板存放地址
	private  static  final String TEMP_PATH="D:\\idea\\botianjinrong\\ruoyi-modules\\ruoyi-contract\\src\\main\\resources\\pdf_template\\个人借款合同.pdf";
	//修改好之后的存放的地址
	private  static  final String DEST="D:\\idea\\botianjinrong\\ruoyi-modules\\ruoyi-contract\\src\\main\\resources\\pdf_template\\个人借款合同1.pdf";
	//文字模板，找电脑的文字模板存放地址
	private  static  final  String FONT_PATH="C:\\Windows\\Fonts\\msyh.ttc,0";


	public static  void  generatePDFUtli(Contract contract) throws IOException {

		PdfDocument pdfDocument = new PdfDocument(new PdfReader(TEMP_PATH), new PdfWriter(DEST));

		PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDocument, false);
		//文字模板
		PdfFont font = PdfFontFactory.createFont(FONT_PATH);
		//文字颜色
		DeviceRgb deviceRgb = new DeviceRgb(0, 0, 0);
		//进行文本填充
		Map<String, String> Map = new HashMap<>();
		Map.put("firstparty",contract.getFirstparty());
		Map.put("secondparty",contract.getSecondparty());
		Map.put("money",String.valueOf(contract.getMoney()));
		Map.put("interestrate",String.valueOf(contract.getInterestrate()));
		Map.put("borrowtime", String.valueOf(contract.getBorrowtime()));
//		Map.put("inallmoney", String.valueOf(contract.getInallmoney()));
		Map.put("firstPartyIdentityCard",contract.getFirstPartyIdentityCard());
		Map.put("secondPartyIdentityCard",contract.getSecondPartyIdentityCard());

		for (java.util.Map.Entry<String, String> entry : Map.entrySet()) {
			//给pdf文档上的那个文本框进行赋值
			acroForm.getFormFields().get(entry.getKey()).setValue(entry.getValue()).setColor(deviceRgb).setFont(font);
		}
		//清除掉pdf文档上的文本框
		acroForm.flattenFields();
		//关闭流
		pdfDocument.close();
	}

}
